package com.school.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.school.entity.HljtClass;
import com.school.entity.HljtGradetime;
import com.school.service.impl.HljtClassServiceIpml;
import com.school.service.impl.HljtGradetimeServiceImpl;
import com.school.util.Message;
import com.school.util.RepUtil;

import net.sf.json.JSONObject;


@Controller
@RequestMapping(value="HljtGradeTimeController")
public class HljtGradeTimeController {

	@Autowired
	private HljtGradetimeServiceImpl hljtGradetimeServiceImpl;
	@Autowired
	private HljtClassServiceIpml hljtClassServiceIpml;
	
	
	/**
	 * 查询年级 上课时间
	 * @param GradeId
	 * @param rep
	 * @return
	 */
	@RequestMapping("/findAllGradeTime")
	@ResponseBody
	public Message findAllGradeTimeAndGrades(Integer GradeId,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
	
		if(null!=GradeId) {
			List<HljtGradetime> HljtGradetime=new ArrayList<>();
			List<HljtGradetime> gradetime=hljtGradetimeServiceImpl.findGradetime(GradeId);
			for (int i = 0; i < 10; i++) {
				HljtGradetime hljtGradetime=new HljtGradetime();
				if(i+1<=gradetime.size()) {
					hljtGradetime.setId(gradetime.get(i).getId());
					hljtGradetime.setStarttime(gradetime.get(i).getStarttime());
					hljtGradetime.setEndtime(gradetime.get(i).getEndtime());
				}else {
					hljtGradetime.setStarttime("");
					hljtGradetime.setEndtime("");
					hljtGradetime.setId(null);
				}
				hljtGradetime.setGradeid(GradeId);
				hljtGradetime.setSection(i+1);
				HljtGradetime.add(hljtGradetime);
			}
			List<HljtClass> grade=hljtClassServiceIpml.findGrade();
			data.put("grade", grade);
			data.put("gradetime", HljtGradetime);
			data.put("success", true);
		}else {
			List<HljtClass> grade=hljtClassServiceIpml.findGrade();
			data.put("grade", grade);
			data.put("success", true);
		}
		Message.setData(data);
		return Message;
		
	}
	/**
	 * 添加修个年级上课时间
	 * @param GradeTime
	 * @param rep
	 * @return  String GradeTime,
	 */
	@RequestMapping("/updateGradeTime")
	@ResponseBody
	public Message updateGradeTime( String GradeTime,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		
			if(null!=GradeTime && GradeTime.length()>0) {
			JSONObject json = JSONObject.fromObject(GradeTime);
			List<JSONObject> list=json.getJSONArray("gradetime");
			
			for (int i = 0; i < list.size(); i++) {
				Integer gradeid =Integer.parseInt(list.get(i).getString("gradeid"));
				Integer section =Integer.parseInt(list.get(i).getString("section"));
				
				List<HljtGradetime> gradetime=hljtGradetimeServiceImpl.getGradetime(gradeid,section );
				if(null!=gradetime&&gradetime.size()>0) {
//					/*修改*/
//					
					HljtGradetime hljtGradetime=new HljtGradetime();
					hljtGradetime.setEndtime(list.get(i).getString("endtime"));
					hljtGradetime.setStarttime(list.get(i).getString("starttime"));
					hljtGradetime.setGradeid(Integer.parseInt(list.get(i).getString("gradeid")));
					hljtGradetime.setSection(Integer.parseInt(list.get(i).getString("section")));
					hljtGradetimeServiceImpl.updateGradetime(hljtGradetime);
					
					List<HljtGradetime> HljtGradetime=new ArrayList<>();
					List<HljtGradetime> gradetimeall=hljtGradetimeServiceImpl.findGradetime(gradeid);
					
					for (int j = 0; j < 10; j++) {
						HljtGradetime hljtGradetimes=new HljtGradetime();
						if(i+1<=gradetimeall.size()) {
							if(null!=gradetimeall.get(j).getId()&&gradetimeall.get(j).getId()>0) {
								
								hljtGradetimes.setId(gradetimeall.get(j).getId());
							}else {
								hljtGradetimes.setId(null);
							}
							if(null!=gradetimeall.get(j).getStarttime()&&gradetimeall.get(j).getStarttime().length()>0) {
								hljtGradetimes.setStarttime(gradetimeall.get(j).getStarttime());
								
							}else {
								hljtGradetimes.setStarttime(null);
							}
							if(null!=gradetimeall.get(j).getEndtime()&&gradetimeall.get(j).getEndtime().length()>0) {
								
								hljtGradetimes.setEndtime(gradetimeall.get(j).getEndtime());
							}else {
								hljtGradetimes.setEndtime(null);
							}
						}else {
							hljtGradetimes.setStarttime("");
							hljtGradetimes.setEndtime("");
							hljtGradetimes.setId(null);
						}
						hljtGradetimes.setGradeid(Integer.parseInt(list.get(i).getString("gradeid")));
						hljtGradetimes.setSection(j+1);
						HljtGradetime.add(hljtGradetimes);
					}
					data.put("gradetime", HljtGradetime);
					
					
					List<HljtClass> grade=hljtClassServiceIpml.findGrade();
					data.put("grade", grade);
					data.put("success", true);
				}else {
					/*添加*/
					HljtGradetime hljtGradetime=new HljtGradetime();
					hljtGradetime.setEndtime(list.get(i).getString("endtime"));
					hljtGradetime.setStarttime(list.get(i).getString("starttime"));
					hljtGradetime.setGradeid(Integer.parseInt(list.get(i).getString("gradeid")));
					hljtGradetime.setSection(Integer.parseInt(list.get(i).getString("section")));
					hljtGradetimeServiceImpl.addGradetime(hljtGradetime);
					
					
					List<HljtGradetime> HljtGradetime=new ArrayList<>();
					List<HljtGradetime> gradetimeall=hljtGradetimeServiceImpl.findGradetime(gradeid);
					if(gradetimeall.size()==list.size()) {
						for (int j = 0; j < i+1; j++) {
							HljtGradetime hljtGradetimes=new HljtGradetime();
							if(i+1<=gradetimeall.size()) {
								hljtGradetimes.setId(gradetimeall.get(j).getId());
								hljtGradetimes.setStarttime(gradetimeall.get(j).getStarttime());
								hljtGradetimes.setEndtime(gradetimeall.get(j).getEndtime());
							}else {
								hljtGradetimes.setStarttime("");
								hljtGradetimes.setEndtime("");
								hljtGradetimes.setId(null);
							}
							hljtGradetimes.setGradeid(gradeid);
							hljtGradetimes.setSection(j+1);
							HljtGradetime.add(hljtGradetimes);
						}
						data.put("gradetime", HljtGradetime);
					}else {
						
					}
					
					List<HljtClass> grade=hljtClassServiceIpml.findGrade();
					data.put("grade", grade);
					data.put("success", true);
				}
			}
		}else {
				data.put("success", false);
			}
		Message.setData(data);
		return Message;
	}

}
